﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PES_TurnierManager.Model;

namespace PES_TurnierManager
{
    /// <summary>
    /// Interaktionslogik für Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public PES_TurnierManager.Model.Model mModel;
        public PES_TurnierManager.Controller.ITournamentController mController;

        public Window1()
        {
            InitializeComponent();
            mGroupControl.NewGroup += new GroupControl.NewGroupAdded(Window_NewGroup_1);
            mGroupControl.MatchResultEntered += new GroupControl.MatchResultEnterdEvent(mGroupControl_MatchResultEntered);
            mGroupControl.RemovePlayerFromGroup += new GroupControl.RemovePlayerFromGroupEvent(mGroupControl_RemovePlayerFromGroup);
        }

        /// <summary>
        /// Reacts if the user wants to remove a player from a group. 
        /// </summary>
        /// <remarks>
        /// This event´s source is the GroupTabItem which passes it to
        /// the GroupControl and the GroupControl passes it to this
        /// Window
        /// </remarks>
        /// <param name="playerName">name of the player to remove</param>
        /// <param name="groupName">name of the players group</param>
        void mGroupControl_RemovePlayerFromGroup(string playerName, string groupName)
        {
            mController.removePlayer(playerName, groupName);
        }

        /// <summary>
        /// Reacts if the GUI wants to enter a match and relays
        /// the action to the controller
        /// Event is fired by GroupControl.
        /// </summary>
        /// <param name="groupName">the group of the match</param>
        /// <param name="matchID">the match ID</param>
        /// <param name="homeScore">score of home team</param>
        /// <param name="guestScore">score of guest team</param>
        void mGroupControl_MatchResultEntered(string groupName, uint matchID, uint homeScore, uint guestScore)
        {
            mController.resultForMatch(groupName, matchID, homeScore, guestScore);
        }

        /// <summary>
        /// Routed Event sent from the NewGroup MenuItem of the
        /// MainTabs Context Menu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_NewGroup_1()
        {
            TextBoxDialog dia = new TextBoxDialog("Neue Gruppe hinzufügen", 
                "Namen der neuen Gruppe hier eingeben");
            dia.ShowDialog();
            if(dia.getDialogResult())
                mController.addGroup(dia.getTextBoxContent(), dia.isLegBoxChecked());
        }

        /// <summary>
        /// Sets the Model for this view
        /// </summary>
        /// <param name="model"></param>
        public void setModel(Model.Model model)
        {
            mModel = model;
            mModel.NewGroup += new PES_TurnierManager.Model.ModelNewGroup(mModel_NewGroup);
            mModel.PlayerAddedToGroup += new PES_TurnierManager.Model.ModelPlayerAddedToGroup(mModel_PlayerAddedToGroup);
            mModel.MatchesSet += new PES_TurnierManager.Model.ModelMatchesSet(mModel_MatchesSet);
            mModel.GroupUpdate += new ModelGroupUpdate(mModel_GroupUpdate);
            mModel.PlayerRemovedFromGroup += new ModelPlayerRemovedFromGroup(mModel_PlayerRemovedFromGroup);
        }

        /// <summary>
        /// Reacts if a player was removed from the model
        /// </summary>
        /// <param name="playerName"></param>
        /// <param name="groupName"></param>
        void mModel_PlayerRemovedFromGroup(string playerName, string groupName)
        {
            mGroupControl.removePlayerFromGroup(playerName, groupName);
        }

        void mModel_GroupUpdate(Group group)
        {
            mGroupControl.updateGroup(group);
        }
        
        /// <summary>
        /// Reacts if the model has generated matches for a group and adds them 
        /// to the view
        /// </summary>
        /// <param name="matches">List aof all matches of a group</param>
        /// <param name="groupName">the group to add the matches to</param>
        void mModel_MatchesSet(List<PES_TurnierManager.Model.Match> matches, string groupName)
        {
            mGroupControl.addMatchesToGroup(matches, groupName);
        }

        /// <summary>
        /// Reqcts if the model has added a player to a group
        /// </summary>
        /// <param name="groupName">the name of the group to add the player to</param>
        /// <param name="player">The player to add</param>
        void mModel_PlayerAddedToGroup(string groupName, HumanPlayer player)
        {
            mGroupControl.addPlayerToGroup(groupName, player.getPlayerName(), player.getTeamName());
        }

        /// <summary>
        /// Reacts on the GroupAdded Event from the Model
        /// </summary>
        /// <remarks>
        /// Since this is a new group (it´s empty) we only the pass the 
        /// name to the view to create a new group
        /// </remarks>
        /// <param name="groupName">name of the group that has been adeed by the model</param>
        void mModel_NewGroup(string groupName)
        {
            GroupTabItem group = mGroupControl.addGroupPanel(groupName);
            group.PlayerAdded += new GroupTabItem.PlayerAddedToGroupEvent(group_PlayerAddedRequest);
            group.GenerateMatches += new GroupTabItem.GenerateMatchesEvent(group_GenerateMatches);
        }

        /// <summary>
        /// Reacts if the player wants to generate the matches for a group
        /// </summary>
        /// <param name="groupName"></param>
        void group_GenerateMatches(string groupName)
        {
            mController.generateMatchesForGroup(groupName);
        }

        /// <summary>
        /// Reacts if the user wants to add a player to a group
        /// </summary>
        /// <param name="groupName">the group to add a player to</param>
        void group_PlayerAddedRequest(string groupName)
        {
            AddPlayerDialog dia = new AddPlayerDialog();
            dia.ShowDialog();
            if (dia.getDialogResult())
            {
                mController.addPlayerToGroup(groupName, dia.getPlayerName(), dia.getTeamName());
            }
        }

        /// <summary>
        /// Sets the Controller for this View
        /// </summary>
        /// <param name="controler"></param>
        public void setController(Controller.ITournamentController controler)
        {
            mController = controler;
        }
    }

    
}
